<template>
  <el-col :span="24">
    <el-button size="small" @click="materialsDialog()">选取物料信息</el-button>
    <el-dialog title="选取物料列表" v-if="visible" append-to-body :visible.sync="visible" center width="1380px" :before-close="handleCancel">
      <div class="page-filter mb10 fl pr">
        <el-form ref="filter" :model="filter" :rules="rules" label-width="0">
          <el-form-item class="mr10 mb5 fl" style="z-index: 999">
            <el-input v-model="filter.name" placeholder="品名" size="small" clearable />
          </el-form-item>
          <el-form-item class="mr10 mb5 fl">
            <el-input v-model="filter.sku" placeholder="规格型号" size="small" clearable />
          </el-form-item>
          <el-form-item class="mr10 mb5 fl">
            <el-input v-model="filter.parameter" placeholder="参数" size="small" clearable />
          </el-form-item>
          <el-form-item class="mr10 mb5 fl" style="width: 450px">
            <el-button class="fl" size="small" @click="reset()">清 空</el-button>
            <el-button class="fl" type="primary" size="small" @click="getList">查 询</el-button>
            <el-button class="fl" type="primary" size="small" @click="handleChoose">选 取</el-button>
          </el-form-item>
          <el-form-item class="pa" style="top: 32px; left: 5px;" prop="multipleSelection" />
        </el-form>
      </div>
      <el-table class="search-table" @selection-change="handleSelectionChange" :data="list" border fit highlight-current-row style="width: 100%;">
        <el-table-column fixed="left" type="selection" width="56px" align="center" /> 
        <el-table-column label="物料编码" width="160px" prop="no" />
        <el-table-column label="品名" width="160px" prop="name" />
        <!-- <el-table-column label="库存" align="right" width="80px">
          <template slot-scope="{row}">
            {{ row.num - row.ex }}
          </template>
        </el-table-column> -->
        <el-table-column label="规格型号" width="260px" prop="sku" />
        <el-table-column label="参数" width="320px" prop="parameter" />
        <el-table-column label="品牌" width="140px" prop="brand" />
        <el-table-column label="单位" width="90px" prop="unit" />
        <el-table-column label="评估价" align="right" width="80px">
          <template slot-scope="{row}">
            {{ app.toFix(row.evaluation_price, 2) }}
          </template>
        </el-table-column>
        <el-table-column label="备注" min-width="260px" prop="remark" />
      </el-table>
      <pagination v-show="filter.pageAll>0" :total="filter.pageAll" :page.sync="filter.pageCurrent" :limit.sync="filter.pageNumber" @pagination="getList" />
    </el-dialog>
  </el-col>
</template>

<script>
import page from '@/utils/page'
import { materials } from '@/api/material'

export default {
  name: 'MaterialsSelect',
  extends: page,
  props: {
    reqList: {
      default: materials,
      type: Function
    },
    getData: {
      type: Function,
      default: function() {}
    },
    value: {
      type: Array,
      default: () => { return [] } 
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.$set(this.filter, 'name', '')
        this.$set(this.filter, 'sku', '')
        this.$set(this.filter, 'parameter', '')
        this.$set(this, 'multipleSelection', [])
        this.getList()
      }
    }
  },
  data() {
    const vnull = (rule, value, callback) => {
      if (this.multipleSelection.length === 0) {
        callback(new Error(rule.message))
      } else {
        callback()
      }
    }
    return {
      multipleSelection: [],
      rules: {
        multipleSelection: [{ required: true, trigger: 'blur', message: '请选取物料', validator: vnull }]
      },
      visible: false
    }
  },
  methods: {
    materialsDialog() {
      this.visible = true
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleChoose() {
      this.$refs['filter'].validate(async(valid) => {
        if (valid){
          if(this.value) this.$emit('input', this.multipleSelection)
          if(this.getData) this.getData(this.multipleSelection)
          this.visible = false
        }
      })
    },
    reset() {
      const data = {}
      data['pageNumber'] = this.filter['pageNumber']
      data['pageCurrent'] = this.filter['pageCurrent']
      data['pageAll'] = this.filter['pageAll']
      this.filter = data
      this.$refs['filter'].resetFields()
    }
  }
}
</script>

<style scoped>
</style>
